package org.dromara.system.controller.system;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysUserCommonConfigBo;
import org.dromara.system.domain.vo.SysUserCommonConfigVo;
import org.dromara.system.service.ISysUserCommonConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代理公共配置
 *
 * @author Lion Li
 * @date 2025-07-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userCommonConfig")
public class SysUserCommonConfigController extends BaseController {

    private final ISysUserCommonConfigService sysUserCommonConfigService;

    /**
     * 查询代理公共配置列表
     */
//    @SaCheckPermission("system:userCommonConfig:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserCommonConfigVo> list(SysUserCommonConfigBo bo, PageQuery pageQuery) {
        return sysUserCommonConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出代理公共配置列表
     */
//    @SaCheckPermission("system:userCommonConfig:export")
    @Log(title = "代理公共配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserCommonConfigBo bo, HttpServletResponse response) {
        List<SysUserCommonConfigVo> list = sysUserCommonConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "代理公共配置", SysUserCommonConfigVo.class, response);
    }

    /**
     * 获取代理公共配置详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:userCommonConfig:query")
    @GetMapping("/{id}")
    public R<SysUserCommonConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long id) {
        return R.ok(sysUserCommonConfigService.queryById(id));
    }

    /**
     * 新增代理公共配置
     */
//    @SaCheckPermission("system:userCommonConfig:add")
    @Log(title = "代理公共配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUserCommonConfigBo bo) {
        return toAjax(sysUserCommonConfigService.insertByBo(bo));
    }

    /**
     * 修改代理公共配置
     */
//    @SaCheckPermission("system:userCommonConfig:edit")
    @Log(title = "代理公共配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserCommonConfigBo bo) {
        return toAjax(sysUserCommonConfigService.updateByBo(bo));
    }

    /**
     * 删除代理公共配置
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:userCommonConfig:remove")
    @Log(title = "代理公共配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysUserCommonConfigService.deleteWithValidByIds(List.of(ids), true));
    }


    //    @SaCheckPermission("system:userCommonConfig:deploy")
    @Log(title = "代理公共配置", businessType = BusinessType.UPDATE)
    @PostMapping("/deploy")
    public R<Void> deploy(@RequestBody SysUserCommonConfigBo bo) {
        return toAjax(sysUserCommonConfigService.deploy(bo));
    }

    @GetMapping("/queryCommonConfig/{userId}")
    public R<SysUserCommonConfigVo> queryCommonConfig(@PathVariable Long userId) {
        return R.ok(sysUserCommonConfigService.queryCommonConfig(userId));
    }

    @GetMapping("/queryWithdrawalWechatReview/{userId}")
    public R<SysUserCommonConfigVo> queryWithdrawalConfig(@PathVariable Long userId) {
        return R.ok(sysUserCommonConfigService.queryWithdrawalConfig(userId));
    }

}
